<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2011
 */

class Admin_MembershipFeeController extends Zend_Controller_Action
{
    private $_membershipFeeService;
    private $_memberService;

    public function init()
    {
        $this->_membershipFeeService = $this->_helper->getService('Core_Service_MembershipFee');
        $this->_memberService = $this->_helper->getService('Core_Service_Member');
    }

    public function indexAction()
    {
        $this->_helper->acl()->isAllowed('service:membershipFee', 'fetch');
        $this->view->years = $this->_membershipFeeService->fetchYears();
    }

    public function createAction()
    {
        $form = new Admin_Form_MembershipFee_AddYear();
        $request = $this->getRequest();

        if ($request->isPost() && $form->isValid($request->getPost()))
        {
            $values = $form->getValues();
            $year = $values['year'];
            return $this->_helper->redirector('edit', null, null, array('year' => $year));
        }

        $this->view->form = $form;
    }

    public function setPaymentAction() {
        // TODO: ACL ACL ACL
        
        $year = $this->_getParam('year');
        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($year)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $year = (int)$year;
        
        $request = $this->getRequest();
        if ($request->isPost()) {
            $values = $request->getPost();

            // Setting members as paid
            if (isset($values['add_payment_for_members'])) {
                $this->_helper->acl()->isAllowed('service:membershipFee', 'create');

                foreach($values['members'] as $memberId) {
                    if (!$validator->isValid($memberId)) {
                        throw new RedCross_Exception_ValidationFailed();
                    }
                    $memberId = (int)$memberId;
                    $member = $this->_memberService->fetchById($memberId);

                    $feeAdded = $this->_memberService->addPayment($memberId, $year);

                    if (is_object($feeAdded)) {
                        $this->view->flashMessage()->addMessage('Betalingsinformasjon for ' . $member->first_name . ' ' . $member->last_name . ' ble oppdatert.', RedCross_View_Helper_FlashMessage::SUCCESS);
                    } else {
                        $this->view->flashMessage()->addMessage('Betalingsinformasjon for ' . $member->first_name . ' ' . $member->last_name . ' ble ikke oppdatert.', RedCross_View_Helper_FlashMessage::ERROR);
                    }
                }
            }

            // Setting members as not paid
            if (isset($values['remove_payment_for_members'])) {
                $this->_helper->acl()->isAllowed('service:membershipFee', 'delete');

                foreach($values['members'] as $memberId) {
                    if (!$validator->isValid($memberId)) {
                        throw new RedCross_Exception_ValidationFailed();
                    }
                    $memberId = (int)$memberId;
                    $member = $this->_memberService->fetchById($memberId);

                    $count = $this->_memberService->removePayment($memberId, $year);

                    if ($count > 0) {
                        $this->view->flashMessage()->addMessage('Betalingsinformasjon for ' . $member->first_name . ' ' . $member->last_name . ' ble oppdatert.', RedCross_View_Helper_FlashMessage::SUCCESS);
                    }
                }
            }
        }

        return $this->_helper->redirector('edit', null, null, array('year' => $year));
    }

    public function editAction()
    {
        $this->_helper->acl()->isAllowed('service:membershipFee', 'fetch');

        $year = $this->_getParam('year');
        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($year)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $year = (int)$year;
        $this->view->year = $year;

        $filterContent = array(
            'has_paid' => 'Medlemmer som har betalt kontingent',
            'not_paid' => 'Medlemmer som ikke har betalt kontingent',
            'all' => 'Alle medlemmer'
        );

        $filterForm = $this->view->filterForm = $this->view->filterForm($filterContent);
        $filterForm->setAction($this->view->url(array('module' => 'admin', 'controller' => 'membership-fee', 'action' => 'edit'), null, false));

        $request = $this->getRequest();

        if ($request->isGet() && $this->_getParam('filter', false) && $filterForm->isValid($request->getQuery())) {
            $value = $filterForm->getValue('filter');
            switch ($value) {
            case 'has_paid':
                $this->view->members = $this->_membershipFeeService->fetchPaidMembersForYear($year);
                break;
            case 'not_paid':
                $this->view->members = $this->membersNotPaid($year);
                break;
            case 'all':
                $this->view->members = $this->_memberService->fetchAllActive();
                break;
            }
        }

        if (!isset($this->view->members)) {
            $this->view->members = $this->_membershipFeeService->fetchPaidMembersForYear($year);
        }
    }

    /*
     * Helper method for aggregating members that has not paid for
     * the givent year. Doctrine did not like the one query approach.
     */
    private function membersNotPaid($year) {
        $hasPaid = $this->_membershipFeeService->fetchPaidMembersForYear($year)->getData();
        $allMembers = $this->_memberService->fetchAllActive()->getData();
        return array_diff($allMembers, $hasPaid);
    }
}

?>
